#!/bin/bash
$APPDIR/func -reset_gtk
export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8

export MODE=$1
OPEN=$2
WINDOW_TITLE="$3"
echo true > $WORKDIR/open_run #this file is removed to close box
echo -n > $WORKDIR/open_output

nav(){
	if [ -d "${OPEN}" ]; then #browse
		AUDIO_FORMATS2="`echo "$AUDIO_FORMATS" | sed 's/\\$/\\\|/g'`" #example: |/path/file.mp3|
		#parent dir
		TMP=${OPEN%/*}
		[ "$OPEN" = ".." ] && OPEN="$TMP"
		if [ "$OPEN" != "/" ]; then echo "$TMP|.." > $WORKDIR/open_list; else echo -n > $WORKDIR/open_list; fi
		#---
		case $MODE in
			track)
				find -L "$OPEN" -mindepth 1 -maxdepth 1 -printf "%p|%f|||||||||||||gtk-%Y\n" | grep -v '/\.' | sort -t'|' -k15,15 | sed -e 's/gtk-d/gtk-directory/g' -e 's/gtk-f/gtk-audio/g' | grep -iE "$AUDIO_FORMATS2|gtk-directory" >> $WORKDIR/open_list
				;;
			playlist)
				find -L "$OPEN" -mindepth 1 -maxdepth 1 -printf "%p|%f|||||||||||||gtk-%Y\n" | grep -v '/\.' | sort -t'|' -k15,15 | sed -e 's/gtk-d/gtk-directory/g' -e 's/gtk-f/gtk-audio/g' | grep -iE "\.pmu\||\.pmd\||\.m3u\||\.pls\||gtk-directory" >> $WORKDIR/open_list
				;;
		esac
		echo "$OPEN" > $WORKDIR/open_search
	else #file is selected
		case $OPEN in [0-9]*) OPEN=${OPEN#*:};; esac #remove unique source-nr 
		echo "$OPEN" > $WORKDIR/open_output
		rm $WORKDIR/open_run
	fi
}

search(){
	case $SEARCH in
		'')
			echo > $WORKDIR/sourcelist
			exit
			;;
		/*) #browse
			OPEN="$SEARCH"
			nav
			;;
		*) #search
			. $APPDIR/func -search_sources "$SEARCH" no_update
			mv -f $WORKDIR/search_sources $WORKDIR/open_list
			echo "$SEARCH" > $WORKDIR/open_search
			;;
	esac
}
export -f nav search

nav
echo "$HOME" > $WORKDIR/open_search

S='
<window icon-name="pmusic" title="'$WINDOW_TITLE'" window_position="2">
 <vbox>
  <entry name="search" height-request="30">
   <variable>SEARCH</variable>
   <input file>'$WORKDIR'/open_search</input>
   <action signal="activate">search</action>
   <action signal="activate">refresh:OPEN</action>
  </entry>

  <vbox space-expand="false" space-fill="false">
   <hbox spacing="5" tooltip-text="'$(gettext 'Define which sources to search')'" space-expand="true" space-fill="true">
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'Search')'</label>
     <input file stock="gtk-find"></input>
     <action>search</action>
     <action>refresh:OPEN</action>
    </button>
    <text width-request="15" space-expand="false" space-fill="false"><label>""</label></text>
    <vbox spacing="2" space-expand="false" space-fill="false">
     <checkbox label="'$(gettext 'My Tracks')'" height-request="15" space-expand="false" space-fill="false">
      <variable>SEARCH_MYMUSIC</variable>'
      [ $MODE = track ] && S=$S'<default>true</default>'
      [ $MODE != track ] && S=$S'<sensitive>false</sensitive>'
     S=$S'</checkbox>
     <checkbox label="'$(gettext 'Collections')'" height-request="15" space-expand="false" space-fill="false">
      <variable>SEARCH_COLLECTIONS</variable>'
      [ $MODE = playlist ] && S=$S'<default>true</default>'
      [ $MODE != playlist ] && S=$S'<sensitive>false</sensitive>'
     S=$S'</checkbox>
    </vbox>
    <vbox spacing="2" space-expand="true" space-fill="true">
     <hbox>
      <checkbox label="'$(gettext 'Radio stations')'" height-request="15" space-expand="false" space-fill="false">
       <variable>SEARCH_RADIO</variable>'
       [ $MODE != track ] && S=$S'<sensitive>false</sensitive>'
      S=$S'</checkbox>
      <checkbox label="jamendo.com" height-request="15" space-expand="true" space-fill="true">
       <variable>SEARCH_JAMENDO</variable>'
       [ $MODE != track ] && S=$S'<sensitive>false</sensitive>'
      S=$S'</checkbox>
     </hbox>
     <hbox spacing="0" space-expand="true" space-fill="true">
      <checkbox label="" height-request="15" space-expand="false" space-fill="false">
       <variable>SEARCH_FILES</variable>'
       ! type pfilesearch > /dev/null 2>&1 && S=$S'<sensitive>false</sensitive>'
       S=$S'<input>echo $SEARCH_FILES</input>
      </checkbox>
      <entry height-request="17" space-expand="true" space-fill="true">
       <variable>SEARCHPATH</variable>
       <default>"'$SEARCHPATH'"</default>'
       ! type pfilesearch > /dev/null 2>&1 && S=$S'<sensitive>false</sensitive>'
      S=$S'</entry>
     </hbox>
    </vbox>
   </hbox>
  </vbox>
 
  <hseparator></hseparator>

  <vbox space-expand="true" space-fill="true">
  
  <tree fixed-height-mode="true" selection-mode="1" sort-function="1" exported_column="0" column-sort-function="0|0|0|0|1|1|0|1|1|0|1|1|0" column-sizing="22|200|150|150|40|60||80|60|70|40|70|" column-resizeable="false|true|" column-header-active="false|true|" column-visible="true|true|true|true|true|true|true|false|true|true|true|true|false|true|false|false|false|false|false|false|false" space-expand="true" space-fill="true">
   <variable>OPEN</variable>
   <height>400</height><width>400</width>
   <label>"|'$(gettext 'Artist / Filename')'                             |'$(gettext 'Title')'                             |'$(gettext 'Album')'                     |'$(gettext 'Nr')'  |'$(gettext 'Year')'     |'$(gettext 'Genre ')'     |'$(gettext 'Comment')'  |'$(gettext 'Rate')'|'$(gettext 'Format')'|'$(gettext 'Kb/s')'  |'$(gettext 'Length')'||'$(gettext 'Path')'                                                                                                                                                                                              ||disc-id musicbrainz.org|||||timestamps"</label>
   <input file stock-column="14">'$WORKDIR'/open_list</input>
   <action>nav</action>
   <action>refresh:OPEN</action>
   <action>refresh:SEARCH</action>
   <action condition="command_is_true([ ! -s $WORKDIR/open_run ] && echo true)">exit:EXIT</action>
  </tree>
  </vbox>

 </vbox>
</window>'

export box_open="$S"
$GTKDIALOG -p box_open
